/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

 /***************************************************************************
 * Name: file_accessor.h
 *
 * Purpose: file accessor(persistency) implementation for base library
 *
 * Developer:
 *   wen.gu , 2019-10-11
 *
 * TODO:
 *
 ***************************************************************************/

 /******************************************************************************
 **    INCLUDES
 ******************************************************************************/
#ifndef __PANDA_PER_FILE_ACCESSOR_H__
#define __PANDA_PER_FILE_ACCESSOR_H__

#include <memory>

#include "panda/core/types.h"
#include "panda/core/errors.h"

namespace panda
{
namespace per
{
/******************************************************************************
 **    MACROS
 ******************************************************************************/

 /******************************************************************************
 **    TYPE DEFINITIONS
 ******************************************************************************/

using int_type = int32_t;
using pos_type = uint32_t;
using off_type = int32_t;


 /******************************************************************************
 **    CLASSES/FUNCTIONS DEFINITIONS
 ******************************************************************************/

class CORE_CLASS FileAccessor
{
public:

    using PandaErrc = panda::core::PandaErrc;
public:
    /** direction of seek */
    enum class SeekDirection
    {
        kBeg = 0,
        kEnd = 1,
        kCur = 2
    };

public:
    virtual ~FileAccessor() {/** todo something */ }

public:
    /**@brief Returns the current position in bytes in the file from start*/
    virtual pos_type tell() = 0;

    /**@brief Sets the position in bytes in the file from beginning */
    virtual PandaErrc seek(pos_type const pos) = 0;

    /**@brief  sets the position in bytes in the file in relation to the SeekDirection */
    virtual PandaErrc seek(off_type const off, SeekDirection const dir) = 0;

    /**@brief Returns the total size of file */
    virtual pos_type size() = 0;
public:  /** for read */
    /**@breif reads n bytes into char pointer and returns actual number
     *        that were read, if < 0, error occurs.
     *        
     */
    virtual int_type read(char* const s, std::size_t const n) = 0;
public: /** for write */
    /**@breif the function flushes and force the OS to write its data to persistent storage */
    virtual void fsync() = 0;

    /** writes n bytes from s and returns actual number of written bytes 
     *  if < 0, error occurs.
     */
    virtual int_type write(char const* const s, std::size_t const n) = 0;
};

} /** namespace per */
} /** namespace panda */

#endif /** !__PANDA_PER_FILE_ACCESSOR_H__ */
